home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / id3 / writers.h < prev   
Encoding:
C/C++ Source or Header  |  2007-04-10  |  5.1 KB  |  194 lines

  1. // -*- C++ -*-
  2. // $Id: writers.h,v 1.11 2002/07/02 22:11:16 t1mpy Exp $
  3.  
  4. // id3lib: a software library for creating and manipulating id3v1/v2 tags
  5. // Copyright 1999, 2000  Scott Thomas Haug
  6.  
  7. // This library is free software; you can redistribute it and/or modify it
  8. // under the terms of the GNU Library General Public License as published by
  9. // the Free Software Foundation; either version 2 of the License, or (at your
  10. // option) any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful, but WITHOUT
  13. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  15. // License for more details.
  16. //
  17. // You should have received a copy of the GNU Library General Public License
  18. // along with this library; if not, write to the Free Software Foundation,
  19. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20.  
  21. // The id3lib authors encourage improvements and optimisations to be sent to
  22. // the id3lib coordinator.  Please see the README file for details on where to
  23. // send such submissions.  See the AUTHORS file for a list of people who have
  24. // contributed to id3lib.  See the ChangeLog file for a list of changes to
  25. // id3lib.  These files are distributed with id3lib at
  26. // http://download.sourceforge.net/id3lib/
  27.  
  28. #ifndef _ID3LIB_WRITERS_H_
  29. #define _ID3LIB_WRITERS_H_
  30.  
  31. #include "id3/writer.h"
  32. #include "id3/id3lib_streams.h"
  33. //#include <string.h>
  34.  
  35. class ID3_CPP_EXPORT ID3_OStreamWriter : public ID3_Writer
  36. {
  37.   ostream& _stream;
  38.   pos_type _beg;
  39.  protected:
  40.   ostream& getWriter() const { return _stream; }
  41.  public:
  42.   ID3_OStreamWriter(ostream& writer) : _stream(writer), _beg(_stream.tellp()) { ; }
  43.   virtual ~ID3_OStreamWriter() { ; }
  44.  
  45.   virtual void close() { ; }
  46.   virtual void flush() { _stream.flush(); }
  47.   
  48.   virtual int_type writeChar(char_type ch)
  49.   {
  50.     _stream.put(ch);
  51.     return ch;
  52.   }
  53.  
  54.   /** Write up to \c len chars into buf and advance the internal position
  55.    ** accordingly.  Returns the number of characters write into buf.
  56.    **/
  57.   virtual size_type writeChars(const char buf[], size_type len)
  58.   { 
  59.     _stream.write(buf, len);
  60.     return len;
  61.   }
  62.   virtual size_type writeChars(const char_type buf[], size_type len)
  63.   {
  64.     _stream.write(reinterpret_cast<const char*>(buf), len);
  65.     return len;
  66.   }
  67.  
  68.   virtual pos_type getBeg() { return _beg; }
  69.   virtual pos_type getCur() { return _stream.tellp(); }
  70. };
  71.  
  72. class ID3_CPP_EXPORT ID3_OFStreamWriter : public ID3_OStreamWriter
  73. {
  74.   ofstream& _file;
  75.  public:
  76.   ID3_OFStreamWriter(ofstream& writer) 
  77.     : ID3_OStreamWriter(writer), _file(writer) { ; }
  78.     
  79.   virtual void close() 
  80.   { 
  81.     _file.close();
  82.   }
  83. };
  84.   
  85. class ID3_CPP_EXPORT ID3_IOStreamWriter : public ID3_Writer
  86. {
  87.   iostream& _stream;
  88.   pos_type  _beg;
  89.  protected:
  90.   iostream& getWriter() const { return _stream; }
  91.  public:
  92.   ID3_IOStreamWriter(iostream& writer) : _stream(writer), _beg(_stream.tellp()) { ; }
  93.   virtual ~ID3_IOStreamWriter() { ; }
  94.  
  95.   virtual void close() { ; }
  96.   virtual void flush() { _stream.flush(); }
  97.   
  98.   virtual int_type writeChar(char_type ch)
  99.   {
  100.     _stream.put(ch);
  101.     return ch;
  102.   }
  103.  
  104.   /** Write up to \c len chars into buf and advance the internal position
  105.    ** accordingly.  Returns the number of characters write into buf.
  106.    **/
  107.   virtual size_type writeChars(const char buf[], size_type len)
  108.   { 
  109.     _stream.write(buf, len);
  110.     return len;
  111.   }
  112.   virtual size_type writeChars(const char_type buf[], size_type len)
  113.   {
  114.     _stream.write(reinterpret_cast<const char*>(buf), len);
  115.     return len;
  116.   }
  117.  
  118.   virtual pos_type getBeg() { return _beg; }
  119.   virtual pos_type getCur() { return _stream.tellp(); }
  120. };
  121.  
  122. class ID3_CPP_EXPORT ID3_FStreamWriter : public ID3_IOStreamWriter
  123. {
  124.   fstream& _file;
  125.  public:
  126.   ID3_FStreamWriter(fstream& writer) 
  127.     : ID3_IOStreamWriter(writer), _file(writer) { ; }
  128.     
  129.   virtual void close() 
  130.   { 
  131.     _file.close();
  132.   }
  133. };
  134.   
  135. class ID3_CPP_EXPORT ID3_MemoryWriter : public ID3_Writer
  136. {
  137.   const char_type* _beg;
  138.   /* */ char_type* _cur;
  139.   const char_type* _end;
  140.  protected:
  141.   void setBuffer(char_type* buf, size_t size)
  142.   {
  143.     _beg = buf;
  144.     _cur = buf;
  145.     _end = buf + size;
  146.   };
  147.  public:
  148.   ID3_MemoryWriter()
  149.   {
  150.     this->setBuffer(NULL, 0);
  151.   }
  152.   ID3_MemoryWriter(char_type buf[], size_t size)
  153.   {
  154.     this->setBuffer(buf, size);
  155.   }
  156.   virtual ~ID3_MemoryWriter() { ; }
  157.   virtual void close() { ; }
  158.   virtual void flush() { ; }
  159.     
  160.   /** Write up to \c len chars from buf and advance the internal position
  161.    ** accordingly.  Returns the number of characters written from buf.
  162.    **/
  163.   virtual size_type writeChars(const char buf[], size_type len)
  164.   { 
  165.     return this->writeChars(reinterpret_cast<const char_type *>(buf), len);
  166.   }
  167.   virtual size_type writeChars(const char_type buf[], size_type len)
  168.   {
  169.     size_type remaining = _end - _cur;
  170.     size_type size = (remaining > len) ? len : remaining;
  171.     ::memcpy(_cur, buf, size);
  172.     _cur += size;
  173.     return size;
  174.   }
  175.     
  176.   virtual pos_type getCur() 
  177.   { 
  178.     return _cur - _beg; 
  179.   }
  180.     
  181.   virtual pos_type getBeg()
  182.   {
  183.     return _beg - _beg;
  184.   }
  185.     
  186.   virtual pos_type getEnd()
  187.   {
  188.     return _end - _beg;
  189.   }
  190. };
  191.  
  192. #endif /* _ID3LIB_WRITERS_H_ */
  193.  
  194.